package com.ztgf.file.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.file.models.bo.FileUserBO;
import com.ztgf.file.models.bo.FileUserSaveBO;
import com.ztgf.file.models.dto.FileUserDTO;
import com.ztgf.file.models.service.IFileUserService;
import com.ztgf.file.models.vo.FileUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 文件用户表控制器
 *
 * @author mxli
 * @Date 2020-10-15 14:32:14
 */

@RestController
@RequestMapping("/cms/fileUser")
@Slf4j
@Api(value = "FileUserController",tags = "文件用户表接口")
public class FileUserController {

    @DubboReference
    private IFileUserService fileUserService;

    @ApiOperation(value = "添加文件用户表",notes = "添加文件用户表")
    @PostMapping(value = "/insert")
    public Result insert(FileUserSaveBO saveBO) {

        if(StrUtils.isEmpty(saveBO.getAccount()) || StrUtils.isEmpty(saveBO.getPassword()) || saveBO.getPassword().length() != 32){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("account", saveBO.getAccount());
        FileUserDTO dto = fileUserService.selectOne(wrapper);
        //验证数据库是否有相同的用户名
        if (dto != null) {
            return new Result(CodeEnum.FAIL_USER_200002);
        }

        boolean flag = fileUserService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存文件用户表",notes = "保存文件用户表")
    public Result save(FileUserSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改文件用户表",notes = "修改文件用户表")
    @PostMapping("/update")
    public Result update(FileUserSaveBO saveBO){
        FileUserDTO dto = new FileUserDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setPassword(null);
        dto.setSalt(null);
        dto.setId(saveBO.getId());
        boolean flag = fileUserService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "修改文件用户表",notes = "修改文件用户表")
    @PostMapping("/updateFileSzie")
    public Result update(Long userId, Long fileSize){
        FileUserDTO dto = new FileUserDTO();
        dto.setFileSize(fileSize);
        dto.setId(userId);
        boolean flag = fileUserService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "修改密码",notes = "修改密码")
    @PostMapping("/updatePassword")
    public Result update(String oldPassword,String newPassword,@RequestParam(value = "name",required = false) String name){
        if(StrUtils.isEmpty(oldPassword) || StrUtils.isEmpty(newPassword)){
            return new Result(CodeEnum.FAIL_100002);
        }
        return fileUserService.updateUserPassword(oldPassword, newPassword, name);
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
             ,@ApiImplicitParam(name = "status",value = "状态 0启用，1冻结、2删除",required = true,paramType = "query", dataType = "Integer")
     })
     public Result updateStatus(Long id, Integer status){
         FileUserDTO dto = new FileUserDTO();
         dto.setId(id);
         dto.setStatus(status);
        boolean flag = fileUserService.updateById(dto);
        if(status == 1){//冻结状态 删除session
            fileUserService.delUserSessionById(id);
        }
         if(flag){
             return new Result<>();
         } else {
             return new Result<>(CodeEnum.FAIL_100008);
         }
     }

    @ApiOperation(value = "删除文件用户表",notes = "删除文件用户表")
    @ApiImplicitParam(name = "id",value = "文件用户表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = fileUserService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除文件用户表",notes = "批量删除文件用户表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        fileUserService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<FileUserDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        FileUserDTO dto = fileUserService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "文件用户表列表",notes = "文件用户表列表")
    @PostMapping("/queryPageList")
    public Result<Page<FileUserDTO>> queryPageList(FileUserBO bo){
        Wrapper<FileUserDTO> wrapper = new EntityWrapper<>();

        if(StrUtils.isNotEmpty(bo.getAccount())){
            wrapper.like("account", bo.getAccount());
        }

        if(StrUtils.isNotEmpty(bo.getName())){
            wrapper.like("name", bo.getName());
        }

        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<FileUserDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = fileUserService.selectPage(page, wrapper);

        if(page.getRecords() != null && page.getRecords().size() > 0){

            for (FileUserDTO dto : page.getRecords()) {
                dto.setPassword(null);
                dto.setSalt(null);
            }
        }

        return new Result<>(page);
    }


    @ApiOperation(value = "用户文件使用大小统计",notes = "用户文件使用大小统计")
    @PostMapping("/queryPageVOList")
    public Result<Page<FileUserVO>> queryPageVOList(FileUserBO bo){
        Wrapper<FileUserVO> wrapper = new EntityWrapper<>();

        if(StrUtils.isNotEmpty(bo.getAccount())){
            wrapper.like("fu.account", bo.getAccount());
        }

        if(StrUtils.isNotEmpty(bo.getName())){
            wrapper.like("fu.name", bo.getName());
        }

        if (bo.getStatus() != null){
            wrapper.eq("fu.status", bo.getStatus());
        }

        if (bo.getStartDate() != null){
            wrapper.ge("fu.create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("fu.create_time", bo.getEndDate());

        }
        Page<FileUserVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = fileUserService.queryPageVOList(page, wrapper,bo.getType());

        return new Result<>(page);
    }
}
